#include "LogWrapper.h"
#include <vector>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>

static const char *LOGGER_FORMAT = "[%Y-%m-%d %T%e][%^%l%$]%v";

LogWrapper *LogWrapper::mInstance = nullptr;

LogWrapper::LogWrapper()
{
    std::vector<spdlog::sink_ptr> sinks;
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::debug);
    console_sink->set_pattern(LOGGER_FORMAT);
    sinks.push_back(console_sink);

    auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("fodlog.txt", 1024 * 1024, 3, false);
    file_sink->set_level(spdlog::level::info);
    file_sink->set_pattern(LOGGER_FORMAT);
    sinks.push_back(file_sink);
    mLogger = std::make_shared<spdlog::logger>("console_file_logger", begin(sinks), end(sinks));
    spdlog::register_logger(mLogger);
    mLogger->set_level(spdlog::level::debug);

    mLogger->flush_on(spdlog::level::err);
}

void LogWrapper::outputLog(const char * fmt, ...)
{
    char tmp[2048] = { 0 };
    va_list args;
    va_start(args, fmt);
    vsprintf_s(tmp, fmt, args);
    va_end(args);
    mLogger->info(tmp);
}


LogWrapper * LogWrapper::getInstance()
{
    if (mInstance == nullptr) {
        mInstance = new LogWrapper();
    }
    return mInstance;
}

LogWrapper::~LogWrapper()
{
    spdlog::drop_all();
}